#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 100005
#define MAXM 100005

typedef struct 
{
    int u, v;
} Edge;

Edge edges[MAXM];
int indeg[MAXN], outdeg[MAXN];
long long dp[MAXN];
int pre_head[MAXN];
int pre_to[MAXM];
int pre_next[MAXM];
int pre_edge_count = 0;
int queue[MAXN];

int cmp(const void* a, const void* b) 
{
    Edge* ea = (Edge*)a;
    Edge* eb = (Edge*)b;
    if (ea->v != eb->v) return ea->v - eb->v;
    return ea->u - eb->u;
}

void add_pre_edge(int v, int u) 
{
    pre_to[pre_edge_count] = u;
    pre_next[pre_edge_count] = pre_head[v];
    pre_head[v] = pre_edge_count++;
}


int main() {
    int n, m;
    scanf_s("%d %d", &n, &m);
    for (int i = 0; i < m; i++)
    {
        scanf_s("%d %d", &edges[i].u, &edges[i].v);
    }

    qsort(edges, m, sizeof(Edge), cmp);

    int unique_m = 0;
    for (int i = 0; i < m; i++)
    {
        if (i == 0 || edges[i].u != edges[i - 1].u || edges[i].v != edges[i - 1].v)
        {
            edges[unique_m++] = edges[i];
        }
    }
    m = unique_m;

    memset(indeg, 0, sizeof(indeg));
    memset(outdeg, 0, sizeof(outdeg));
    memset(pre_head, -1, sizeof(pre_head));
    pre_edge_count = 0;

    for (int i = 0; i < m; i++)
    {
        int u = edges[i].u;
        int v = edges[i].v;
        outdeg[u]++;
        indeg[v]++;
        add_pre_edge(v, u);
    }

    int front = 0, rear = 0;

    for (int i = 1; i <= n; i++)
    {
        if (indeg[i] == 0) 
        {
            queue[rear++] = i;
            dp[i] = 1;
        }
        else
        {
            dp[i] = 0;
        }
    }

    while (front < rear)
    {
        int v = queue[front++];
        for (int i = pre_head[v]; i != -1; i = pre_next[i])
        {
            int u = pre_to[i];
            dp[u] += dp[v];
            indeg[u]--;
            if (indeg[u] == 0)
            {
                queue[rear++] = u;
            }
        }
    }

    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (outdeg[i] == 0) 
        {
            ans += dp[i];
        }
    }
    printf("%lld\n", ans);

    return 0;
}